package io.iec.edp.caf.rpc.config;

import io.grpc.BindableService;
import io.iec.edp.caf.rpc.api.event.RpcClientEventBroker;
import io.iec.edp.caf.rpc.api.filter.RpcClientFilter;
import io.iec.edp.caf.rpc.api.filter.RpcServerFilter;
import io.iec.edp.caf.rpc.api.service.CAFRpcClient;
import io.iec.edp.caf.rpc.api.service.InternalServiceManageService;
import io.iec.edp.caf.rpc.client.CAFRpcClientImpl;
import io.iec.edp.caf.rpc.client.local.RpcLocalInvoker;
import io.iec.edp.caf.rpc.remote.grpc.GrpcServerInvoke;
import io.iec.edp.caf.rpc.remote.grpc.filter.GrpcClientFilter;
import io.iec.edp.caf.rpc.remote.grpc.filter.GrpcServerFilter;
import io.iec.edp.caf.rpc.remote.grpc.registry.GrpcServiceRegistryListener;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;


public class GrpcAutoConfiguration {

//    @Order(value = Ordered.HIGHEST_PRECEDENCE)
//    @Bean
//    public ApplicationListener grpcStartupListener(ServiceRegistry serviceRegistry, MsuProperties configuration) {
//        return new GrpcServiceRegistryListener(serviceRegistry,configuration);
//    }
    @Order(value = Ordered.HIGHEST_PRECEDENCE)
    @Bean
    public ApplicationListener grpcStartupListener() {
        return new GrpcServiceRegistryListener();
    }

//    @Bean
//    public CAFRpcClient cafRpcClient(RpcClientEventBroker clientEventBroker,
//                                     InternalServiceManageService management,
//                                     RpcLocalInvoker rpcLocalInvoker,
//                                     ITenantService tenantService,
//                                     ITenantRouteService tenantRouteService){
//        return new CAFRpcClientImpl(clientEventBroker,management,rpcLocalInvoker,tenantService,tenantRouteService);
//    }

    @Bean
    public CAFRpcClient cafRpcClient(RpcClientEventBroker clientEventBroker,
                                     InternalServiceManageService management,
                                     RpcLocalInvoker rpcLocalInvoker){
        return new CAFRpcClientImpl(clientEventBroker,management,rpcLocalInvoker);
    }

    @Bean
    public BindableService grpcServerInvoke(){
        return new GrpcServerInvoke();
    }

    @Bean
    public RpcServerFilter cafGrpcServerInterceptor(){
        return new GrpcServerFilter();
    }

    @Bean
    public RpcClientFilter cafGrpcClientInterceptor(){
        return new GrpcClientFilter();
    }
}
